package com.ruoyi.appointment.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.appointment.domain.BusServiceItem;
import com.ruoyi.appointment.domain.BusStatementItem;
import com.ruoyi.appointment.domain.vo.BusStatementVo;
import com.ruoyi.appointment.mapper.BusStatementItemMapper;
import com.ruoyi.appointment.service.IBusAppointmentService;
import com.ruoyi.appointment.service.IBusServiceItemService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import com.ruoyi.appointment.mapper.BusStatementMapper;
import com.ruoyi.appointment.domain.BusStatement;
import com.ruoyi.appointment.service.IBusStatementService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * 结算单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-10
 */

@Service
public class BusStatementServiceImpl implements IBusStatementService {
    @Autowired
    private BusStatementMapper busStatementMapper;
    @Autowired
    private IBusServiceItemService busServiceItemService;
    @Autowired
    private BusStatementItemMapper busStatementItemMapper;
    @Lazy
    @Autowired
    private IBusAppointmentService busAppointmentService;

    /**
     * 查询结算单
     *
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public BusStatement selectBusStatementById(Long id) {
        return busStatementMapper.selectBusStatementById(id);
    }

    /**
     * 查询结算单列表
     *
     * @param busStatement 结算单
     * @return 结算单
     */
    @Override
    public List<BusStatement> selectBusStatementList(BusStatement busStatement) {
        return busStatementMapper.selectBusStatementList(busStatement);
    }

    /**
     * 新增结算单
     *
     * @param busStatementVo 结算单
     * @return 结果
     */
    @Override
    public int insertBusStatement(BusStatementVo busStatementVo) {
        BusStatement busStatement = new BusStatement();
        BeanUtils.copyBeanProp(busStatement, busStatementVo);
        return this.insertBusStatement(busStatement, null);
    }


    /**
     * 新增结算单
     *
     * @param busStatement
     * @param appointmentId 预约信息的id
     * @return
     */
    @Override
    public int insertBusStatement(BusStatement busStatement, Long appointmentId) {


        busStatement.setAppointmentId(appointmentId);
        busStatement.setStatus(BusStatement.CONSUMER);
        busStatement.setIsDelete(Constants.DEL_FLAG_NORMAL);
        busStatement.setCreaterTime(DateUtils.getNowDate());
        return busStatementMapper.insertBusStatement(busStatement);

    }

    /**
     * 修改结算单
     *
     * @param busStatementVo 结算单
     * @return 结果
     */
    @Override
    public int updateBusStatement(BusStatementVo busStatementVo) {
        BusStatement busStatement = selectBusStatementById(busStatementVo.getId());
        Assert.notNull(busStatement, "参数错误");
        Assert.state(BusStatement.CONSUMER.equals(busStatement.getStatus()), "已支付的订单不能修改");
        BeanUtils.copyBeanProp(busStatement, busStatementVo);
        return busStatementMapper.updateBusStatement(busStatement);
    }

    /**
     * 批量删除结算单
     *
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementByIds(Long[] ids) {
        int rows = busStatementMapper.deleteBusStatementByIds(ids);
        Assert.state(rows > 0, "已支付结算单不能删除");
        return rows;
    }

    /**
     * 删除结算单信息
     *
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementById(Long id) {
        int rows = busStatementMapper.deleteBusStatementById(id);
        Assert.state(rows > 0, "已支付结算单不能删除");
        return rows;
    }

    /**
     * 新增结算单明细
     *
     * @param busStatementVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertItems(BusStatementVo busStatementVo) {
        List<BusStatementItem> busStatementItems = busStatementVo.getItems();
        Assert.state(!CollectionUtils.isEmpty(busStatementItems), "请选择本次消费的服务项");
        BusStatement busStatement = selectBusStatementById(busStatementVo.getId());
        Assert.state(BusStatement.CONSUMER.equals(busStatement.getStatus()), "只能修改消费中的结算单");
        BigDecimal totalAmount = BigDecimal.ZERO;
//        BigDecimal totalQuantity = BigDecimal.ZERO;
        Integer totalQuantity = 0;
        for (BusStatementItem busStatementItem : busStatementItems) {
            BusServiceItem busServiceItem = busServiceItemService.selectBusServiceItemById(busStatementItem.getItemId());
            Assert.notNull(busServiceItem, "服务项不存在" + busServiceItem.getName());
            busStatementItem.setItemName(busServiceItem.getName());
            busStatementItem.setItemPrice(busServiceItem.getDiscountPrice());
            BigDecimal amount = busStatementItem.getItemPrice().multiply(new BigDecimal(busStatementItem.getItemQuantity()))
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            totalAmount = totalAmount.add(amount);
//            totalQuantity = totalQuantity.add(new BigDecimal(busStatementItem.getItemQuantity()));
            totalQuantity += busStatementItem.getItemQuantity();
        }
        busStatement.setId(busStatementVo.getId());
        busStatement.setDiscountAmount(busStatementVo.getDiscountAmount());
        busStatement.setTotalMount(totalAmount);
        busStatement.setTotalQuantity(totalQuantity);
        int rows = busStatementMapper.updateBusStatement(busStatement);
        Assert.state(rows > 0, "更新结算单失败");
        busStatementItemMapper.deleteBusStatementItemById(busStatementVo.getId());
        rows = busStatementItemMapper.batchInsertBusStatementItem(busStatementItems);
        Assert.state(rows > 0, "新增结算单失败");
        return rows;
    }

    @Override
    public List<BusStatementItem> selectItemsById(Long statementId) {

        return busStatementItemMapper.selectBusStatementItemList(statementId);
    }

    /**
     * 预支付
     *
     * @param id
     * @return
     */
    @Override
    public int doPrepay(Long id) {
        BusStatement busStatement = selectBusStatementById(id);
        Assert.state(BusStatement.CONSUMER.equals(busStatement.getStatus()), "该订单已支付");
        Long userId = SecurityUtils.getUserId();
        int rows = busStatementMapper.paySuccess(id, userId);
        Assert.state(rows > 0, "支付失败");
        if (busStatement.getAppointmentId() != null) {
            rows = busAppointmentService.updatePaySuccess(busStatement.getAppointmentId());
            Assert.state(rows > 0, "更新预约信息状态失败");
        }
        return rows;
    }

    /**
     * 根据预约信息id获取结算单
     *
     * @param appointmentId
     * @return
     */
    @Override
    public BusStatement selectBusStatementByAppointmentId(Long appointmentId) {

        return busStatementMapper.selectBusStatementByAppointmentId(appointmentId);
    }
}
